<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use App\Services\Tools\CommonService;
use Illuminate\Support\Facades\DB;  //引用DB
use App\Services\Tools\FileService;   //引入文件处理类
class SociatyPermissionRepository
{
    /**
     * 根据type获得所有menuid的字串
     * @param $type
     * @return string
     */
    public function getMenusStrByType($type)
    {
        $menus = app('model_sc_menu')->where('type',$type)->select('menu_id')->get();
        $str = '';
        foreach($menus as &$val)
        {
            $str .= $val->menu_id.',';
        }
        //去掉最后一位
        $str = substr($str, 0, -1);

        return $str;
    }

    /**
     * 根据菜单字串返回所有权限id字串
     * @param $menuStr
     * @return string
     */
    public function getPermissionStrByMenuId($menuStr)
    {
        $permissions = app('model_sc_permission')->whereIn('menu_id',explode(',',$menuStr))->select('permission_id')->get();
        $str = '';
        foreach($permissions as &$val)
        {
            $str .= $val->permission_id.',';
        }
        //去掉最后一位
        $str = substr($str, 0, -1);

        return $str;
    }

    /**
     * 添加角色
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function roleAdd($param)
    {
        $res = app('model_sc_role')->create($param);
        if($res) return $res;
        throw new BasicException(90004);
    }
}